<?php
namespace Ctrl;

use Lib;
use Lib\Paas\Login;

use Model\ChessModel;
use Model\RaceModel;
use Model\JobModel;
class Chess extends Lib\Base\Ctrl
{
    private $db;
    private $version;
    private $season;
    private $where;
    private $chessModel;
    public function __construct()
    {

        //校验管理员权限
        $roleService = new Lib\Paas\Role();
        $checkAdmin = $roleService->getRoleInfo();
//        if ($checkAdmin['role'] != ROLE_ID) {
//            die('抱歉！您没有访问权限');
//        }
//        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
//            die('抱歉！您没有访问权限');
//        }
        $this->db = Lib\Db\DbModel::init();

        //获取当前版本
        $this->version = $this->db->table('tbversion')->where(['isCurrent'=>1])->find('id');
        $this->season = $this->db->table('tbseason')->where(['isCurrent'=>1])->find('id');
        /*if (empty($this->version) || empty($this->season)){
            header("Location: ". U('main/main'));
        }*/
        $this->where = [
            'versionId' =>$this->version['id'],
            'seasonId' =>$this->season['id'],
            'isDelete'=>0
        ];
        $this->assign('iActId','1001');

        $this->chessModel = new ChessModel();
    }

    //棋子视图首页
    public function index()
    {
        //种族信息
        $raceModel = new RaceModel();
        $races = $raceModel->getFormatRace($this->where);
        //职业信息
        $jobModel = new JobModel();
        $jobs = $jobModel->getFormatJob($this->where);

        $keywords = I('post.keywords');
        if ($keywords && !empty($keywords)) {
            $this->where['displayName'] = ['like','%'.$keywords.'%'];
        }

        //获取种族所有种族信息
        $chess = $this->chessModel->getAllChess($this->where);

        if (I('post.')) {
            $this->ajaxSuccess("成功",$chess);
        }

        $TFTIDs = [];
        foreach ($chess as $k=>$v) {
            $TFTIDs[$v['TFTID']] = "https://game.gtimg.cn/images/lol/act/img/tft/champions/".$v['name'];
        }


        /*if( empty($chess)) {
            header("Location: ". U('main/main'));
        }*/

        $this->assign('races',json_encode($races));
        $this->assign('jobs',json_encode($jobs));
        $this->assign('chess',$chess);
        $this->assign('TFTIDs',json_encode($TFTIDs));

        $this->display('Admin/Chess/index');
    }

    public function getSynergiesList()
    {
        $chessId = I('chessId');
        $where = $this->where;
        if (isset($chessId) && !empty($chessId)) {
            $where['chessId'] = ['!=',$chessId];
        }
        $chess = $this->chessModel->getAllChess($where);
        foreach ($chess as $k=> $v) {
            $chess[$k]['jobIds'] = explode(",",$v['jobIds']);
            $chess[$k]['raceIds'] = explode(",",$v['raceIds']);
        }
        $data = ['SynergiesList'=>$chess];
        $this->ajaxSuccess('success',$data);


//        $this->display('Admin/Chess/synergies');
    }


    //获取单个棋子的信息
    public function getChessInfo()
    {
        $chessId = I('chessId');
        $chess = $this->chessModel->getChessInfo($chessId,$this->where);
        if(!empty($chess)) {
            $this->ajaxSuccess("成功",$chess);

        } else{
            $this->ajaxError('获取失败');
        }

    }

    //修改种族信息
    public function update()
    {
        $chessId = I('chessId');
        $post = I('post.');
        if (!is_numeric($post['TFTID'])) {
            $this->ajaxError('TFTID必须为数字类型');
        }

        if (!is_numeric($post['price'])) {
            $this->ajaxError('棋子费用必须为数字类型');
        }
        if (!is_numeric($post['spellBlock'])) {
            $this->ajaxError('魔抗必须为数字类型');
        }
        if (!is_numeric($post['attackSpeed'])) {
            $this->ajaxError('攻击速度必须为数字类型');
        }
        if (!is_numeric($post['magic'])) {
            $this->ajaxError('法力必须为数字类型');
        }
        if (!is_numeric($post['attackRange'])) {
            $this->ajaxError('攻击范围必须为数字类型');
        }
        if (!is_numeric($post['crit'])) {
            $this->ajaxError('暴击率必须为数字类型');
        }
        if (!is_numeric($post['armor'])) {
            $this->ajaxError('护甲必须为数字类型');
        }
        if (!is_numeric($post['attack'])) {
            $this->ajaxError('初始攻击力必须为数字类型');
        }
        if (!is_numeric($post['attackMag'])) {
            $this->ajaxError('攻击力系数必须为数字类型');
        }
        if (!is_numeric($post['life'])) {
            $this->ajaxError('初始生命值必须为数字类型');
        }
        if (!is_numeric($post['lifeMag'])) {
            $this->ajaxError('生命值系数必须为数字类型');
        }

        //检查当前赛季和版本TFTID是否重复
        if($this->chessModel->checkTFTID($chessId,$this->where,$post['TFTID'])){
            $this->ajaxError('该TFTID已存在');
        }


        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data = [
            'TFTID'=>$post['TFTID'],
            'title'=>$post['title'],
            'name'=>$post['name'],
            'displayName'=>$post['displayName'],
            'raceIds'=>$post['raceIds'],
            'jobIds'=>$post['jobIds'],
            'price'=>$post['price'],
            'skillName'=>$post['skillName'],
            'skillType'=>$post['skillType'],
            'skillImage'=>$post['skillImage'],
            'skillIntroduce'=>$post['skillIntroduce'],
            'originalImage'=>$post['originalImage'],
            'skillDetail'=>$post['skillDetail'],
            'life'=>$post['life'],
            'magic'=>$post['magic'],
            'armor'=>$post['armor'],
            'spellBlock'=>$post['spellBlock'],
            'attackMag'=>$post['attackMag'],
            'attack'=>$post['attack'],
            'attackSpeed'=>$post['attackSpeed'],
            'attackRange'=>$post['attackRange'],
            'crit'=>$post['crit'],
            'updateMan'=>$userName,
            'updateTime'=>date('Y-m-d H:i:s'),
            'synergies'=> $post['synergies'],
            'illustrate' => $post['illustrate']
        ];
        $res =  $this->chessModel->updateChess($chessId,$this->where,$data);

        logWrite(parent::LOG_TYPE_EDIT,$res,'tbchess');
        if($res!==false){

            $this->ajaxSuccess("编辑成功");

        } else {

            $this->ajaxError('编辑失败');
        }
    }


    public function add()
    {
        //种族信息
        $raceModel = new RaceModel();
        $races = $raceModel->getFormatRace($this->where);
        //职业信息
        $jobModel = new JobModel();
        $jobs = $jobModel->getFormatJob($this->where);

        //获取种族所有种族信息
        $chess = $this->chessModel->getAllChess($this->where);
        $TFTIDs = [];
        foreach ($chess as $k=>$v) {
            $TFTIDs[$v['TFTID']] = "https://game.gtimg.cn/images/lol/act/img/tft/champions/".$v['name'];
        }

        $this->assign('races',json_encode($races));
        $this->assign('jobs',json_encode($jobs));
        $this->assign('TFTIDs',json_encode($TFTIDs));
        $this->display('Admin/Chess/add');
    }

    public function addSave()
    {
        $post = I('post.');
        if (!is_numeric($post['chessId'])) {
            $this->ajaxError('棋子费用必须为数字类型');
        }
        if (!is_numeric($post['price'])) {
            $this->ajaxError('棋子费用必须为数字类型');
        }
        if (!is_numeric($post['spellBlock'])) {
            $this->ajaxError('魔抗必须为数字类型');
        }
        if (!is_numeric($post['attackSpeed'])) {
            $this->ajaxError('攻击速度必须为数字类型');
        }
        if (!is_numeric($post['magic'])) {
            $this->ajaxError('法力必须为数字类型');
        }
        if (!is_numeric($post['attackRange'])) {
            $this->ajaxError('攻击范围必须为数字类型');
        }
        if (!is_numeric($post['crit'])) {
            $this->ajaxError('暴击率必须为数字类型');
        }
        if (!is_numeric($post['armor'])) {
            $this->ajaxError('护甲必须为数字类型');
        }
        if (!is_numeric($post['attack'])) {
            $this->ajaxError('初始攻击力必须为数字类型');
        }
        if (!is_numeric($post['attackMag'])) {
            $this->ajaxError('攻击力系数必须为数字类型');
        }
        if (!is_numeric($post['life'])) {
            $this->ajaxError('初始生命值必须为数字类型');
        }
        if (!is_numeric($post['lifeMag'])) {
            $this->ajaxError('生命值系数必须为数字类型');
        }

        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data = [
            'TFTID'=>$post['TFTID'],
            'chessId'=>$post['chessId'],
            'title'=>$post['title'],
            'name'=>$post['name'],
            'displayName'=>$post['displayName'],
            'raceIds'=>$post['raceIds'],
            'jobIds'=>$post['jobIds'],
            'price'=>$post['price'],
            'skillName'=>$post['skillName'],
            'skillType'=>$post['skillType'],
            'skillImage'=>$post['skillImage'],
            'skillIntroduce'=>$post['skillIntroduce'],
            'skillDetail'=>$post['skillDetail'],
            'originalImage'=>$post['originalImage'],
            'life'=>$post['life'],
            'magic'=>$post['magic'],
            'armor'=>$post['armor'],
            'spellBlock'=>$post['spellBlock'],
            'attackMag'=>$post['attackMag'],
            'attack'=>$post['attack'],
            'attackSpeed'=>$post['attackSpeed'],
            'attackRange'=>$post['attackRange'],
            'crit'=>$post['crit'],
            'creater'=>$userName,
            'createTime'=>date('Y-m-d H:i:s'),
            'synergies'=> $post['synergies'],
            'illustrate' => $post['illustrate']

        ];

        $where = [
            'versionId' => $this->version['id'],
            'seasonId' => $this->season['id'],
            'isDelete' => 1
        ];
        if ($this->chessModel->getChessInfo($post['chessId'],$where)) {
            $this->ajaxError('该棋子已存在');
        }

        if($this->chessModel->checkTFTID($post['chessId'],$this->where,$post['TFTID'])){
            $this->ajaxError('该TFTID已存在');
        }
        $res =  $this->chessModel->insertChessOne($this->version['id'],$this->season['id'],$data);

        logWrite(parent::LOG_TYPE_INSENT,$res,'tbchess');
        if($res!==false){

            $this->ajaxSuccess("添加成功");

        } else {

            $this->ajaxError('添加失败');
        }
    }
}